//
//  MBSpaceMyAlbumCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/22.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceSetCell.h"

@interface MBSpaceSetCell () {
    UIView                  *_bgView;
    UIImageView             *_nickImgView;
    UILabel                 *_nickNameLb;
    UIView                  *_lineView;
    UIButton                *_contentBt;
    UISwitch                *_switch;
}

@end
@implementation MBSpaceSetCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    //头像
    _nickImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _nickImgView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:_nickImgView];
    
    //标题
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    [_bgView addSubview:_nickNameLb];
    
    //开关
    _switch = [[UISwitch alloc]initWithFrame:CGRectZero];
    _switch.backgroundColor = [UIColor clearColor];
    _switch.hidden = YES;
    _switch.on = YES;
    [_switch addTarget:self action:@selector(onSwitchSelectAction:) forControlEvents:UIControlEventValueChanged];
    [_bgView addSubview:_switch];
    
    _contentBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _contentBt.backgroundColor = [UIColor clearColor];
    [_contentBt setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
    [_bgView addSubview:_contentBt];
    
    //线
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColorFromHex(0x999999) colorWithAlphaComponent:.35];
    [_bgView addSubview:_lineView];
    
}

- (void)setIsShowSwitch:(BOOL)isShowSwitch
{
    _isShowSwitch = isShowSwitch;
    [self setNeedsLayout];
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (void)setIsShowSwitchOn:(BOOL)isShowSwitchOn
{
    _isShowSwitchOn = isShowSwitchOn;
    [self setNeedsLayout];
}

//开关
- (void)onSwitchSelectAction:(UISwitch *)switchON
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectSwitchONForDownMusic:withView:)]) {
        [_delegate onSelectSwitchONForDownMusic:switchON.on withView:self];
    }
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (_dataDic) {
        //value
        _nickImgView.image = [UIImage imageNamed:[_dataDic objectForKey:@"pic"]];
        _nickNameLb.text = [_dataDic objectForKey:@"text"];
        _contentBt.hidden = _isShowSwitch;
        _switch.hidden = !_contentBt.isHidden;
        _switch.on = _isShowSwitchOn;
        
        //frame
        _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height);
        _nickImgView.frame = CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (_bgView.height-25*PERCEN)/2., 25*PERCEN, 25*PERCEN);
        
        _nickNameLb.frame = CGRectMake(_nickImgView.right+20*PERCEN, (_bgView.height-20)/2. ,240, 20);
        
        _contentBt.frame = CGRectMake(_bgView.width-70*PERCEN-(ISIPAD?440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), (_bgView.height-50)/2., 70*PERCEN, 50);
        
        _switch.frame = CGRectMake(_bgView.width-60-(ISIPAD?440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), (_bgView.height-30)/3., 60, 30);
        
        _lineView.frame = CGRectMake((ISIPAD?440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), _bgView.height-1, _bgView.width-(ISIPAD?880*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), 1);
    }
}

@end
